﻿using System;
using System.Collections.Generic;
using System.Collections;

namespace MBT.Utils
{
    /// <summary>
    /// Some utility functions for working with Lists
    /// </summary>
    public class ListUtil
    {
        /// <summary>
        /// [Instead of List, we may use EnList which has "Equals"]
        /// Determine that two lists are equal or not
        /// </summary>
        /// <param name="l1"></param>
        /// <param name="l2"></param>
        /// <returns></returns>
        public static bool EqualLists(IList l1, IList l2)
        {
            if (l1.Count != l2.Count)
                return false;
            for (int i = 0; i < l1.Count; i++)
                if (!l1[i].Equals(l2[i]))
                    return false;
            return true;
        }

        /// <summary>
        /// Write the content of a list in console
        /// </summary>
        /// <param name="l1"></param>
        /// <param name="separator"></param>
        public static void WriteList(IList l1, string separator = ",")
        {
            if (l1 == null) { Console.Write("TestResultType: Null list!"); return; }
            for (int i = 0; i < l1.Count; i++)
                Console.Write((i > 0 ? separator : "") + l1[i].ToString());
        }

        /// <summary>
        /// Conver a list to string
        /// </summary>
        /// <param name="l1">the list</param>
        /// <param name="separator">separator between elements</param>
        /// <returns>the generated string</returns>
        public static string ListToString(IList l1, string separator = ",")
        {
            string temp = "";
            if (l1 == null) { return "<>"; }
            for (int i = 0; i < l1.Count; i++)
                temp += ((i > 0 ? separator : "") + (l1[i] == null ? "NULL" : l1[i].ToString()));
            return temp;
        }


        /// <summary>
        /// The maximum index till which two lists are equal
        /// </summary>
        /// <param name="l1">first list</param>
        /// <param name="l2">second list</param>
        /// <returns></returns>
        public static int PrefixLength(IList l1, IList l2)
        {
            int min = Math.Min(l1.Count, l2.Count);
            for (int i = 0; i < min; i++)
            {
                if (!l1[i].Equals(l2[i]))
                    return i;
            }
            return min;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="l"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static List<T> Prefix<T>(List<T> l, int len)
        {
            List<T> p = new List<T>();
            for (int i = 0; i < len; i++)
                p.Add(l[i]);
            return p;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="l"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static List<T> Suffix<T>(List<T> l, int len)
        {
            List<T> p = new List<T>();
            for (int i = l.Count - len; i < l.Count; i++)
                p.Add(l[i]);
            return p;
        }
    }
}
